package com.taiji.dianthus.sms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taiji.dianthus.domain.sms.SmsAccount;
import com.taiji.dianthus.domain.sms.SmsInfo;
import com.taiji.dianthus.util.EncryptUtil;
import com.taiji.dianthus.util.RestUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


@Service
public class HttpSendSmsServiceImpl implements HttpSendSmsService {

    @Autowired
    private RestUtil restUtil;


    @Override
    public JSONObject sendDSMS(SmsInfo smsInfo, SmsAccount smsAccount) {
        JSONObject oo = new JSONObject();
        try {
            StringBuilder content = new StringBuilder();
            if (!StringUtils.isEmpty(smsInfo.getMsgSubject())) {
                content.append("【" + smsInfo.getMsgSubject() + "】");
            }
            content.append(smsInfo.getMsgContent());
            String code = getSmsParam(smsInfo.getReceiveUserPhone(), content.toString(), smsAccount);
            oo = restUtil.post(SmsConstant.URL, code);
            System.out.println(oo);
            //   apiService.doPost(SmsConstant.URL, getSmsParam(smsInfo));
        } catch (Exception e) {
            e.printStackTrace();
            oo.put("success", false);
            oo.put("rspcod", "ConnectFalse");
        }
        return oo;
    }

    public static String getSmsParam(String mobiles, String content, SmsAccount smsAccount) throws Exception {
        Submit submit = new Submit();
        submit.setEcName(smsAccount.getEcName());
        submit.setApId(smsAccount.getUserAccount());
        submit.setSecretKey(smsAccount.getUserPassword());
        submit.setMobiles(mobiles);
        submit.setContent(content);
        submit.setSign(smsAccount.getSign());
        submit.setAddSerial(smsAccount.getExtendNumber());
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submit.getEcName());
        stringBuffer.append(submit.getApId());
        stringBuffer.append(submit.getSecretKey());
        stringBuffer.append(submit.getMobiles());
        stringBuffer.append(submit.getContent());
        stringBuffer.append(submit.getSign());
        stringBuffer.append(submit.getAddSerial());
       // System.out.println(stringBuffer.toString());
        String selfMac = EncryptUtil.MD5encrypt(stringBuffer.toString());
       // System.out.println("selfMac:" + selfMac);
        submit.setMac(selfMac);
        String parem = JSON.toJSONString(submit);
        System.out.println("param:" + parem);
        String encode = Base64.encodeBase64String(parem.getBytes());
        System.out.println(encode);
        return encode;


    }

    public static void main(String[] args) {
        JSONObject oo = new JSONObject();
        oo.put("ecName", "政企分公司测试");
        oo.put("apId", "demo0");
        oo.put("mobiles", "13800138000");
        oo.put("content", "移动改变生活。");
        oo.put("sign", "DWItALe3A");
        oo.put("addSerial", "");
        oo.put("mac", "7997ddb079db2155b517b21b2a812370");
        try {
            //  String body = Base64.getEncoder().encodeToString(str.getBytes("UTF-8"));
            // System.out.println(Base64.encodeBase64String(oo.toJSONString().getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
       /* SmsDomain smsDomain = new SmsDomain();
        smsDomain.setMobiles("15101630605");
        smsDomain.setSmsContent("测试短信");
        try {
            getSmsParam(smsDomain);
        } catch (Exception e) {
            e.printStackTrace();
        }*/


    }

    /**
     * 字符串转unicode
     *
     * @param str
     * @return
     */
    public static String stringToUnicode(String str) {
        StringBuffer sb = new StringBuffer();
        char[] c = str.toCharArray();
        for (int i = 0; i < c.length; i++) {
            sb.append("\\u" + Integer.toHexString(c[i]));
        }
        return sb.toString();
    }
}
